<?php
/**
 * Created by PhpStorm.
 * User: 宇
 * Date: 2016/9/23
 * Time: 8:46
 * 管理员类
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\admin\model\Admin;

class Index extends Controller
{

    //管理员后台登录
    public function adminLogin()
    {
        //判断是否登录

        if (input('?post.dosubmit')) {
            //过滤
            $input_list['username'] = Request::instance() -> param('username');
            $input_list['password'] = Request::instance() -> param('password');
            //检查是否为空
            $input = array();
            $input_val = new \app\common\controller\Index();
            foreach ($input_list as $k => $v){
                if (empty($v)) {
                    return 10002;
                }
                $input[$k] = $input_val -> validateInput($v);
            }
            //查询数据库验证登录
            $where = array(
                'username' => $input['username'],
                'password' => $input['password']
            );
            $login_info = model('Admin') -> get($where);
            unset($where);
            if (empty($login_info)){
                $this->error('10004');
            } else {
                Session::set('username',$login_info['username']);
                Session::set('adminid',$login_info['id']);
                Session::set('lastIp',$login_info['lastIp']);
                Session::set('lastTime',$login_info['lastTime']);

                //将登录信息加入数据库
                $where = array(
                    'id' => $login_info['id']
                );
                $data['lastIp'] =  $_SERVER["REMOTE_ADDR"];
                $data['lastTime'] = time();
                $login_base_info = Admin::where($where) -> update($data);
                $this->success('登录成功', 'Index/adminIndex');
                //exit();
            }
        }
        $this->view->replace([
            '__PUBLIC__'    =>  '../../static/',
            '__ROOT__'      =>  '/daogou/public/'
        ]);
        return $this->fetch('adminLogin');
    }

    /*
     * 管理员注册
     * @author: 刘啸宇
     * @date: 2016-9-23 11:16:02
     * input $request
     * return mixed
     */
    public function adminRegister(Request $request)
    {
        if (input('?post.dosubmit')) {
            $data_list['username'] = $request ->post('username');
            $data_list['password'] = $request ->post('password');
            $register_code = $request ->post('registercode');

            //从数据库中获取注册码

            $md5_obj = model('Code') -> where(['code_name' => 'res_code']) ->field('code') -> find();
            $md5 = $md5_obj -> toArray();
            // TODO 后台可以设置注册码,存成md5,(基于注册码给予后台权限) 使用注册码后将注册码使用次数+1

//            dump(md5($register_code));
//            dump($md5['code']);
//            exit();

            if($md5['code'] !== md5($register_code)) {
                return 10001;
            }
            $input_val = new \app\common\controller\Index();
            $data = array();
            foreach ($data_list as $k => $v){
                if (empty($v)) {
                    return 10002;
                }
                $data[$k] = $input_val -> validateInput($v);
            }
            //dump($data);
            //插入数据库
            $excute = model('Admin') ->save($data);
            if(!$excute){
                return 10003;
            } else {
                $this->success('注册成功!','admin/Index/adminLogin');
            }
        }
        return $this->fetch('adminRegister');
    }

    /*
     * 后台首页
     */
    public function adminIndex()
    {
        if (!(Session::has('username'))){
            return 10005;
        } else {
            $username = Session::get('username');

//            $list = Content::where([ 'del' => 0 ])->paginate(5);
//            $this->assign('list',$list);
            $this->assign('username',$username);
            $this->view->replace([
                '__PUBLIC__'    =>  '../../static/',
                '__ROOT__'      =>  '/daogou/public/'
            ]);
            return $this->fetch('adminIndex');
        }
    }
    /*
     * 管理员管理
     */
    public function adminManegerList()
    {
        if (!(Session::has('username'))){
            return 10005;
        } else {
            //获取管理员成员列表
            $admin_obj = Admin::all();
            $admin_arr = array();
            foreach($admin_obj as $k => $v){
                $admin_arr[$k] = $v ->toArray();
            }
//            dump($admin_arr);
            $this->assign('admin_arr',$admin_arr);
            $this->view->replace([
                '__PUBLIC__'    =>  '../../static/',
                '__ROOT__'      =>  '/daogou/public/'
            ]);
            return $this->fetch('content/adminManeger');
        }
    }
    /*
     * 管理员删除
     */
    public function adminDel(Request $request)
    {
        if (!(Session::has('username'))){
            return 10005;
        } else {
            $id = $request -> get('id');
            //将id过滤
            $input_val = new \app\common\controller\Index();
            $id = $input_val ->validateInput($id);
            //操作数据库删除
            $where = array('id', $id);
            $excute = Admin::destroy($where);
            if (0 !== $excute){
                $this->success('删除成功！','admin/Index/adminManegerList');
            } else {
                $this->error('删除失败！','admin/Index/adminManegerList');
            }
        }
        return 0;
    }
    /*
     * 新增管理员
     */
    public function goodsAdd(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $_dosubmit = $request -> post('dosubmit');
            if($_dosubmit) {
                $data['username'] = $request->post('username');
                $data['password'] = $request->post('password');
                $data_arr = array();
                $input_val = new \app\common\controller\Index();

                foreach ($data as $k => $value) {
                    $data_arr[$k] = $input_val->validateInput($value);
                }
                //插入数据库
                $excute = Admin::create($data_arr);
                if (!empty($excute)) {
                    $this->success('新增管理员成功！','admin/Index/adminManegerList');
                } else {
                    $this->error('新增失败！','admin/Index/adminManegerList');
                }
            } else {
                $this->view->replace([
                    '__PUBLIC__'    =>  '../../static/',
                    '__ROOT__'      =>  '/daogou/public/'
                ]);
                return $this->fetch('content/adminAdd');
            }
        }
        return 0;
    }


    /*
     * 管理员登出
     */
    public function adminlogout()
    {
        if(!(Session::has('username'))) {
            $this->error('未登出');
        } else {
            Session::destroy();
            $this->success('您已登出','admin/index/adminlogin');
        }
    }
}